package com.bond.cloud.producer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bond.cloud.dao.TestDataMapper;
import com.bond.cloud.po.TestData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TestServiceImpl {

    @Autowired
    private TestDataMapper testDataMapper;

    public List<TestData> listTest(TestData query) {
        LambdaQueryWrapper<TestData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(TestData::getId, 0);
        return testDataMapper.getByWrapper(queryWrapper);
    }

    public TestData getById(Long id) {
        return testDataMapper.getById(id).get();
    }

    public Page<TestData> myPage(Page<TestData> page) {
        return testDataMapper.myPage(page);
    }


    public Page<TestData> myPage(TestData pageParam, int pageNum, int pageSize) {
        Page<TestData> page = new Page<>(pageNum, pageSize);
        QueryWrapper<TestData> queryWrapper = new QueryWrapper<>();
        if (pageParam.getTestStr() != null) {
            queryWrapper.like("test_str", pageParam.getTestStr());
        }
        page.addOrder(OrderItem.desc("create_time"));
        // 调用 selectPage 方法
        Page<TestData> userPage = testDataMapper.selectPage(page, queryWrapper);
        //List<TestData> userList = userPage.getRecords();
        return userPage;
    }


    public TestData save(TestData testData) {
        testDataMapper.insert(testData);
        return testData;
    }
}
